home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
12 - Game Aesthetics
/
MenuBarRoutines.c
Wrap
Text File
|
1995-06-21
|
5KB
|
163 lines
//==========================================================================//
// //
// MenuBarRoutines.c //
// //
// A couple of handy routines for //
// hiding and showing the menu bar. //
// //
//==========================================================================//
short mBarHigh; // A global variable used by both these routines.
void HideMenuBar (void);
void ShowMenuBar (void);
//-------------------------------------------------------------- HideMenuBar
void HideMenuBar (void)
{
// This function hides the menu bar by
// calling LMSetMBarHeight() and then painting
// over the menu bar with black.
Rect theRect;
RgnHandle worldRgn, menuBarRgn;
GrafPtr wasPort, tempPort;
if (LMGetMBarHeight() != 0) // only hide if not hidden
{
// First, we’re going to save a pointer to the old
// port and create a temporary “scratch port” in
// which to define our regions and paint over the
// menu bar.
GetPort(&wasPort); // remember old port
tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
OpenPort(tempPort); // create temporary new port
SetPort((GrafPtr)tempPort);
// We get a copy of the height of the menu bar and
// save it off in the global mBarHigh (a short).
mBarHigh = LMGetMBarHeight();
// Now we tell the Mac the menu bar is gone.
LMSetMBarHeight(0); // sets low-mem global to 0
// Now we need to get a region that describes the
// familiar rounded rectangle of the main monitor.
// In cases with multiple monitors, the global
// qd.screenBits will be the one with the menu bar...
// The one whose region we’re concerned with
// imitating.
theRect = (**GetGrayRgn()).rgnBBox;
UnionRect(&theRect, &qd.screenBits.bounds, &theRect);
worldRgn = NewRgn(); // init new region
OpenRgn(); // open region for defining
FrameRoundRect(&theRect, 16, 16);
CloseRgn(worldRgn); // just defined the screen
// Although the region we’ve just defined represents
// the entire monitor with the menu bar, we’re going
// to trim this down to a region that represents
// just the menu bar -- the top strip of the screen.
theRect = qd.screenBits.bounds;
theRect.bottom = theRect.top + mBarHigh;
menuBarRgn = NewRgn(); // define yet another region
RectRgn(menuBarRgn, &theRect);
// Now we have worldRgn = the whole screen and
// menuBarRgn = just the top strip where the menu bar
// is. By finding their intersection, we manufacture
// an exact shape of the menu bar -- rounded corners
// only on the top.
SectRgn(worldRgn, menuBarRgn, menuBarRgn);
DisposeRgn(worldRgn); // no longer need this region
// Now that we have our menu bar region, we need to
// add it to our temporary ports’ visRgn. In this
// way we can use QuickDraw to paint over it.
UnionRgn(tempPort->visRgn,
menuBarRgn,
tempPort->visRgn);
DisposeRgn(menuBarRgn); // no longer need this region
PaintRect(&theRect); // paint over the menu bar
ClosePort(tempPort); // clean up
SetPort((GrafPtr)wasPort);
}
}
//-------------------------------------------------------------- ShowMenuBar
// Showing the menu bar is, unfortunately, just as complex (only in reverse):
void ShowMenuBar (void)
{
Rect theRect;
GrafPtr wasPort, tempPort;
RgnHandle worldRgn, menuBarRgn;
if (LMGetMBarHeight() == 0) // only do if menu bar hidden
{
GetPort(&wasPort); // do the port thing again
tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
OpenPort(tempPort);
SetPort((GrafPtr)tempPort);
// Restore the menu bar height in low-mem global
LMSetMBarHeight(mBarHigh);
// Create a screen region again (with rounded corners)
theRect = (**GetGrayRgn()).rgnBBox;
UnionRect(&theRect, &qd.screenBits.bounds, &theRect);
worldRgn = NewRgn();
OpenRgn();
FrameRoundRect(&theRect, 16, 16);
CloseRgn(worldRgn);
// Create a rectangular menu bar region again
theRect = qd.screenBits.bounds;
theRect.bottom = theRect.top + mBarHigh;
menuBarRgn = NewRgn();
RectRgn(menuBarRgn, &theRect);
// Intersect the 2 regions to arrive at a rounded
// menu bar region again.
SectRgn(worldRgn, menuBarRgn, menuBarRgn);
DisposeRgn(worldRgn);
// Once again we add it to our tempPort’s visRgn
UnionRgn(tempPort->visRgn,
menuBarRgn,
tempPort->visRgn);
// But this time we “subtract” the menu bar region
// from the tempPort’s visRgn -- in this way, the
// menu bar is removed and thus can’t be drawn over.
DiffRgn(tempPort->visRgn, menuBarRgn, tempPort->visRgn);
DisposeRgn(menuBarRgn);
ClosePort(tempPort); // clean up
SetPort((GrafPtr)wasPort);
// At this point, all is well and good, but the Mac
// hasn’t got around to drawing the menu bar. We need
// to tell it to do so.
DrawMenuBar();
}
}